﻿$(document).ready(function() {

    $("#SetCommunity").click(function(event) {
        
        var address = $("#ZipCode").attr("value");
        centerOnAddress(address)
        event.preventDefault();
    });

    initialize()

    google.load("maps", "2", { "other_params": "sensor=false" });
});


var map;
var geocoder;
var address;

function initialize() {
    map = new GMap2(document.getElementById("map_canvas"));
    map.setMapType(G_HYBRID_MAP);
    map.setCenter(new GLatLng(40.730885, -73.997383), 20);
    map.addControl(new GLargeMapControl);
    GEvent.addListener(map, "click", getAddress);


    // Enable the additional map types within
    //the map type collection
    map.enableRotation();

    //Add the geo coder
    geocoder = new GClientGeocoder();

    //Add the map controls
    var mapControl = new GMapTypeControl();
    map.addControl(mapControl);
    map.addControl(new GLargeMapControl());
}

function getAddress(overlay, latlng) {
    if (latlng != null) {
        address = latlng;
        geocoder.getLocations(latlng, showAddress);
    }
}

function showAddress(response) {
    map.clearOverlays();
    if (!response || response.Status.code != 200) {
        alert("Status Code:" + response.Status.code);
    } else {
        place = response.Placemark[0];
        point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
        marker = new GMarker(point);
        map.addOverlay(marker);
        marker.openInfoWindowHtml(
        '<b>orig latlng:</b>' + response.name + '<br/>' +
        '<b>latlng:</b>' + place.Point.coordinates[1] + "," + place.Point.coordinates[0] + '<br>' +
        '<b>Status Code:</b>' + response.Status.code + '<br>' +
        '<b>Status Request:</b>' + response.Status.request + '<br>' +
        '<b>Address:</b>' + place.address + '<br>' +
        '<b>Accuracy:</b>' + place.AddressDetails.Accuracy + '<br>' +
        '<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode);


        $("#Latitude").attr({
            value: place.Point.coordinates[1]
        });

        $("#Longitude").attr({
            value: place.Point.coordinates[0]
        });

        $("#City").attr({
            value: place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.SubAdministrativeAreaName
        });

        $("#State").attr({
            value: place.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName
        });

        $("#Address").attr({
            value: place.address
        });

        $("#Accuracy").attr({
            value: place.AddressDetails.Accuracy
        });

        var zip = "";

        try {
            zip = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.DependentLocality.PostalCode.PostalCodeNumber;
        }
        catch (err) {
            //alert(err);
        }

        if (zip == "") {
            zip = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber;
        }

        //alert(zip);
        $("#ActualZipCode").attr({
            value: zip
        });

    }
}

function centerOnAddress(address) {
    geocoder.getLatLng(
    address,
    function(point) {
        if (!point) {
            alert(address + " not found");
        } else {
            map.setCenter(point, 18);
            //            var marker = new GMarker(point);
            //            map.addOverlay(marker);
            //            marker.openInfoWindowHtml(address);
        }
    }
  );
}